package services

import (
	"blog-server/app/common/request"
	"blog-server/app/models"
	"blog-server/global"
	"blog-server/utils"
	"errors"
)

type shareServices struct {
}

var ShareService = new(shareServices)

// InsetShare 新增文章
func (shareServices *shareServices) InsetShare(params request.ShareModel) (err error) {
	switch params.ArtType {
	case 1:
		err = InsetMusicShare(params)
		return
	case 2:
		err = InsetTechnologyShare(params)
		return
	case 3:
		err = InsetCultureShare(params)
		return
	default:
		err = errors.New("文章类型错误")
		return
	}
}

func InsetMusicShare(params request.ShareModel) (err error) {
	base := models.MusicContent{
		Content:  params.Content,
		VideoUrl: params.VideoUrl,
		MusicShare: models.MusicShare{
			PageName: params.PageName,
			Remark:   params.Remark,
			CoverUrl: params.CoverUrl,
			Spacial:  params.Spacial,
			ArtType:  params.ArtType,
			Tags:     params.Tags,
			Author:   params.Author,
		},
	}
	err = global.App.DB.Create(&base).Error
	return
}

func InsetTechnologyShare(params request.ShareModel) (err error) {
	base := models.TechnologyContent{
		Content:  params.Content,
		VideoUrl: params.VideoUrl,
		TechnologyShare: models.TechnologyShare{
			PageName: params.PageName,
			Remark:   params.Remark,
			CoverUrl: params.CoverUrl,
			Spacial:  params.Spacial,
			ArtType:  params.ArtType,
			Tags:     params.Tags,
			Author:   params.Author,
		},
	}
	err = global.App.DB.Create(&base).Error
	return
}

func InsetCultureShare(params request.ShareModel) (err error) {
	base := models.CultureContent{
		Content:  params.Content,
		VideoUrl: params.VideoUrl,
		CultureShare: models.CultureShare{
			PageName: params.PageName,
			Remark:   params.Remark,
			CoverUrl: params.CoverUrl,
			Spacial:  params.Spacial,
			ArtType:  params.ArtType,
			Tags:     params.Tags,
		},
	}
	err = global.App.DB.Create(&base).Error
	return
}

// QueryShareList 查询文章列表
func (shareServices *shareServices) QueryShareList(params request.ShareList) (list interface{}, err error) {
	switch params.ArtType {
	case 1:
		list, err = QueryMusicList(params)
		return
	case 2:
		list, err = QueryTechnologyList(params)
		return
	case 3:
		list, err = QueryCultureList(params)
		return
	default:
		err = errors.New("找不到文章类型")
		return
	}
}

func QueryMusicList(params request.ShareList) (list []models.MusicShare, err error) {
	if params.PageName == "" {
		err = global.App.DB.Scopes(utils.Paginate(params.PageNum, params.PageSize)).Find(&list).Error
		return
	}
	err = global.App.DB.Scopes(utils.Paginate(params.PageNum, params.PageSize)).Where("page_name", params.PageName).Find(&list).Error
	return
}

func QueryTechnologyList(params request.ShareList) (list []models.TechnologyShare, err error) {
	if params.PageName == "" {
		err = global.App.DB.Scopes(utils.Paginate(params.PageNum, params.PageSize)).Find(&list).Error
		return
	}
	err = global.App.DB.Scopes(utils.Paginate(params.PageNum, params.PageSize)).Where("page_name", params.PageName).Find(&list).Error
	return
}

func QueryCultureList(params request.ShareList) (list []models.CultureShare, err error) {
	if params.PageName == "" {
		err = global.App.DB.Scopes(utils.Paginate(params.PageNum, params.PageSize)).Find(&list).Error
		return
	}
	err = global.App.DB.Scopes(utils.Paginate(params.PageNum, params.PageSize)).Where("page_name", params.PageName).Find(&list).Error
	return
}

// 查看文章详情
func (shareServices *shareServices) QueryShareDetail(params request.ShareDetail) (detail interface{}, err error) {
	switch params.ArtType {
	case 1:
		detail, err = QueryMusicDetail(params)
		return
	case 2:
		detail, err = QueryTechnologyDetail(params)
		return
	case 3:
		detail, err = QueryCultureDetail(params)
		return
	default:
		err = errors.New("找不到文章类型")
		return
	}
}

func QueryMusicDetail(params request.ShareDetail) (detail models.MusicContent, err error) {
	err = global.App.DB.Preload("MusicShare").First(&detail, params.ContentId).Error
	return
}

func QueryTechnologyDetail(params request.ShareDetail) (detail models.TechnologyContent, err error) {
	err = global.App.DB.Preload("TechnologyShare").First(&detail, params.ContentId).Error
	return
}
func QueryCultureDetail(params request.ShareDetail) (detail models.CultureContent, err error) {
	err = global.App.DB.Preload("CultureShare").First(&detail, params.ContentId).Error
	return
}

// 更新文章
func (shareServices *shareServices) UpdateShare(params request.ShareModel) (err error) {
	switch params.ArtType {
	case 1:
		err = UpdateMusicShare(params)
		return
	case 2:
		err = UpdateTechnologyShare(params)
		return
	case 3:
		err = UpdateCultureShare(params)
		return
	default:
		err = errors.New("找不到文章类型")
		return
	}
}

func UpdateMusicShare(params request.ShareModel) (err error) {
	base := models.MusicShare{
		Author:   params.Author,
		PageName: params.PageName,
		Remark:   params.Remark,
		CoverUrl: params.CoverUrl,
		Spacial:  params.Spacial,
		ArtType:  params.ArtType,
		Tags:     params.Tags,
	}
	content := models.MusicContent{
		Content:  params.Content,
		VideoUrl: params.VideoUrl,
	}
	err = global.App.DB.Model(&models.MusicContent{}).Where("content_id", params.ContentId).Updates(&content).Error
	if err != nil {
		return
	}
	err = global.App.DB.Model(&models.MusicShare{}).Where("music_content_content_id", params.ContentId).Updates(&base).Error
	return
}

func UpdateTechnologyShare(params request.ShareModel) (err error) {
	base := models.TechnologyShare{
		Author:   params.Author,
		PageName: params.PageName,
		Remark:   params.Remark,
		CoverUrl: params.CoverUrl,
		Spacial:  params.Spacial,
		ArtType:  params.ArtType,
		Tags:     params.Tags,
	}
	content := models.TechnologyContent{
		Content:  params.Content,
		VideoUrl: params.VideoUrl,
	}
	err = global.App.DB.Model(&models.TechnologyContent{}).Where("content_id", params.ContentId).Updates(&content).Error
	if err != nil {
		return
	}
	err = global.App.DB.Model(&models.TechnologyShare{}).Where("technology_content_content_id", params.ContentId).Updates(&base).Error
	return
}
func UpdateCultureShare(params request.ShareModel) (err error) {
	base := models.CultureShare{
		Author:   params.Author,
		PageName: params.PageName,
		Remark:   params.Remark,
		CoverUrl: params.CoverUrl,
		Spacial:  params.Spacial,
		ArtType:  params.ArtType,
		Tags:     params.Tags,
	}
	content := models.CultureContent{
		Content:  params.Content,
		VideoUrl: params.VideoUrl,
	}
	err = global.App.DB.Model(&models.CultureContent{}).Where("content_id", params.ContentId).Updates(&content).Error
	if err != nil {
		return
	}
	err = global.App.DB.Model(&models.CultureShare{}).Where("culture_content_content_id", params.ContentId).Updates(&base).Error
	return
}

// 上下架
func (shareServices *shareServices) UpdateShareStatus(params request.ShareSelf) (err error) {
	switch params.ArtType {
	case 1:
		err = UpdateMusicStatus(params)
		return
	case 2:
		err = UpdateTechnologyStatus(params)
		return
	case 3:
		err = UpdateCultureStatus(params)
		return
	default:
		err = errors.New("找不到文章类型")
		return
	}
}

func UpdateMusicStatus(params request.ShareSelf) (err error) {
	var model models.MusicShare
	err = global.App.DB.Model(&model).Where("page_id", params.PageId).Update("status", params.Status).Error
	return
}

func UpdateTechnologyStatus(params request.ShareSelf) (err error) {
	var model models.TechnologyShare
	err = global.App.DB.Model(&model).Where("page_id", params.PageId).Update("status", params.Status).Error
	return
}
func UpdateCultureStatus(params request.ShareSelf) (err error) {
	var model models.CultureShare
	err = global.App.DB.Model(&model).Where("page_id", params.PageId).Update("status", params.Status).Error
	return
}

// 删除文章
func (shareServices *shareServices) DeleteShare(params request.ShareSelf) (err error) {
	switch params.ArtType {
	case 1:
		err = DeleteMusic(params)
		return
	case 2:
		err = DeleteTechnology(params)
		return
	case 3:
		err = DeleteCulture(params)
		return
	default:
		err = errors.New("找不到文章类型")
		return
	}
}

func DeleteMusic(params request.ShareSelf) (err error) {
	err = global.App.DB.Where("page_id", params.PageId).Delete(&models.MusicShare{}).Error
	return
}

func DeleteTechnology(params request.ShareSelf) (err error) {
	err = global.App.DB.Where("page_id", params.PageId).Delete(&models.TechnologyShare{}).Error
	return
}

func DeleteCulture(params request.ShareSelf) (err error) {
	err = global.App.DB.Where("page_id", params.PageId).Delete(&models.CultureShare{}).Error
	return
}
